package com.neuedu.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.neuedu.model.UserMessage;
import com.neuedu.service.UserMessageService;
import com.neuedu.util.StringUtil;

@Controller
@RequestMapping(value = "/UserMessage")
public class UserMessageAction {
	@Autowired
	private UserMessageService usermessageService;

	// 下拉框
	@RequestMapping(value = "/findList")
	public void findList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<UserMessage> list = usermessageService.findList();

		String str = JSONObject.toJSONString(list);
		response.getWriter().write(str);
	}

	// *****************************************
	// 登录
	@RequestMapping(value = "/login")
	public void login(UserMessage usermessage, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		UserMessage ret = usermessageService.findUserByName(usermessage);
		if (ret != null) {
			// 登录成功，记录用户的session会话中，并跳转dao管理首页
			request.getSession().setAttribute("INDEX_LOGIN", ret);
			response.sendRedirect(request.getContextPath() + "/IndexWeb/findIndex.do");
		} else {
			// 登录失败，返回登录页面
			response.sendRedirect(request.getContextPath() + "/pages/userLogin.jsp");
		}
	}

	// 查询所有
	@RequestMapping(value = "/findAll")
	public void findAll(UserMessage userMessage,HttpServletRequest request, HttpServletResponse response) throws Exception {

		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> pageMap = new HashMap<String, Object>();
		
		pageMap.put("userName", StringUtil.formatString(userMessage.getUserName()));
		pageMap.put("startPage", (page - 1) * rows); // 从第几条数据开始查询
		pageMap.put("endPage", rows); // 每页查询多少条记录

		List<UserMessage> um = usermessageService.findAll(pageMap);

		int total = usermessageService.countUserMessage();

		map.put("rows", um);
		map.put("total", total);

		String str = JSONObject.toJSONString(map);

		response.getWriter().write(str);
	}

	// 添加
	@RequestMapping(value = "addUserMessage")
	public void addCollect(UserMessage userMessage, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int res = usermessageService.addUserMessage(userMessage);
		System.out.println(res);
		Map<String, Object> map = new HashMap<String, Object>();
		if (res > 0) {
			// map.put("success", true);
			response.sendRedirect(request.getContextPath() + "/pages/userLogin.jsp");
		} else {
			map.put("success", false);
			map.put("errorMsg", "Save user fail !!!");
		}
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);

	}

	// 修改
	@RequestMapping(value = "updateUserMessage")
	public void updateUserMessage(UserMessage usermessage, HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		int res = usermessageService.updateUserMessage(usermessage);
		Map<String, Object> map = new HashMap<String, Object>();
		if (res > 0) {
			map.put("success", true);
		} else {
			map.put("success", false);
			map.put("errorMsg", "update user fail !!!");
		}
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
}
